<template>
  <div class="scenario-card-list">
    <el-empty v-if="scenarios.length === 0" description="暂无场景数据" />
    <el-row v-else :gutter="20">
      <el-col v-for="scenario in scenarios" :key="scenario.id" :xs="24" :sm="12" :md="8" :lg="6" style="margin-bottom: 5px;">
        <el-card shadow="hover" class="scenario-card">
          <div class="scenario-image">
            <el-image :src="scenario.cover" fit="cover" />
            <div class="scenario-tag">
              <el-tag type="success">
                {{ getTypeName(scenario.scene_name) }}
              </el-tag>
            </div>
          </div>
          <div class="scenario-info">
            <h3 class="scenario-name">{{ scenario.scene_name }}</h3>
            <p class="scenario-desc">{{ scenario.scene_desc }}</p>
            <div class="scenario-buses">
              <div class="label">适用总线：</div>
              <div class="content">
                <el-tag v-for="(bus, index) in scenario.scene_dbus_type" :key="index" size="small" class="bus-tag">
                  {{ bus }}
                </el-tag>
              </div>
            </div>
            <div class="scenario-actions">
              <el-button type="primary" size="small" @click="viewScenario(scenario)">查看详情</el-button>
              <el-button type="success" size="small" @click="createPlan(scenario)">创建方案</el-button>
              <el-button type="primary" size="small" @click="editScenarioHandler(scenario)">编辑场景</el-button>
              <el-button type="danger" size="small" @click="deleteScenarioHandler(scenario.id, true)">删除场景</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
/**
 * 场景卡片列表组件
 * @file index.vue
 */
import { defineProps, defineEmits, inject } from 'vue'
import { useRouter } from 'vue-router'

const props = defineProps({
  /**
   * 场景数据数组
   * @type {Array}
   */
  scenarios: {
    type: Array,
    required: true
  }
})

const emit = defineEmits(['view-scenario'])
const router = useRouter()

// 注入父组件提供的方法
const editScenario = inject('editScenario', null)
const deleteScenario = inject('deleteScenario', null)

/**
 * 查看场景详情
 * @param {Object} scenario - 场景对象
 */
const viewScenario = (scenario) => {
  emit('view-scenario', scenario)
}

/**
 * 创建方案
 * @param {Object} scenario - 场景对象
 */
const createPlan = (scenario) => {
  router.push({
    path: '/tactical-plan/create',
    query: { scenarioId: scenario.id }
  })
}

/**
 * 编辑场景
 * @param {Object} scenario - 场景对象
 */
const editScenarioHandler = (scenario) => {
  if (editScenario) {
    editScenario(scenario)
  }
}

/**
 * 删除场景
 * @param {number} id - 场景ID
 */
const deleteScenarioHandler = (id, status) => {
  if (deleteScenario) {
    deleteScenario(id, status)
  }
}

/**
 * 获取场景类型名称
 * @param {string} type - 场景类型
 * @returns {string} 场景类型的中文名称
 */
const getTypeName = (type) => {
  const nameMap = {
    drone: '无人机场景',
    industrial: '工业自动化',
    avionics: '航电场景'
  }
  return nameMap[type] || type
}
</script>

<style lang="scss" scoped>
.scenario-card-list {
  .scenario-card {
    height: 100%;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
    }

    .scenario-image {
      position: relative;
      height: 160px;
      overflow: hidden;

      .el-image {
        width: 100%;
        height: 100%;
      }

      .scenario-tag {
        position: absolute;
        top: 10px;
        right: 10px;
      }
    }

    .scenario-info {
      padding-top: 10px;

      .scenario-name {
        margin: 10px 0;
        font-size: 16px;
        font-weight: 500;
        color: var(--text-color);
      }

      .scenario-desc {
        color: var(--text-color-secondary);
        font-size: 14px;
        line-height: 1.5;
        overflow: hidden;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3;
        margin-bottom: 10px;
      }

      .scenario-buses {
        display: flex;
        margin-bottom: 15px;


        .label {
          color: var(--text-color-secondary);
          font-size: 14px;
          min-width: 70px;
        }

        .content {
          display: flex;
          flex-wrap: wrap;
          gap: 5px;

          .bus-tag {
            margin-right: 5px;
          }
        }
      }

      .scenario-actions {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
      }
    }
  }
}
::v-deep(.el-button+.el-button) {
  margin-left: 0;
  margin-right: 10px;
  margin-bottom: 10px;
}
</style>